//
//  ReferViewController.m
//  项目三
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "ReferViewController.h"
#import "JLbusinessTableViewCell.h"
#import "JLBigTableViewCell.h"
#import "YearView.h"
#import "MonthView.h"
#import "DayView.h"
#import "DuraingView.h"
#import "WhichMoneyView.h"

static NSString*const kUITableViewBusinessCellIndentifier = @"BSCell";
static NSString*const kUITableViewBigCellIndentifier = @"BigCell";

@interface ReferViewController () <UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> {

    YearView *_yearView;
    MonthView *_monthView;
    DayView *_dayView;
    DuraingView *_duraingView;
    WhichMoneyView *_moneyView;
    NSString *_year;
    NSString *_day;
    NSString *_month;
    NSString *_money;
    NSString *_during;
}
@property (nonatomic, strong) UITableView *tableView;

@property (strong, nonatomic) IBOutlet UIView *mmView;
@property (strong, nonatomic) IBOutlet UITextField *persentField;
@property (strong, nonatomic) IBOutlet UITextField *moneyField;
@property (strong, nonatomic) IBOutlet UIButton *mYearBtn;
@property (strong, nonatomic) IBOutlet UIButton *mMonthBtn;
@property (strong, nonatomic) IBOutlet UIButton *mDayBtn;
@property (strong, nonatomic) IBOutlet UIButton *mBtn;


@property (strong, nonatomic) IBOutlet UIView *bigView;
@property (strong, nonatomic) IBOutlet UITextField *locationField;
@property (strong, nonatomic) IBOutlet UITextField *bigMoneyField;
@property (strong, nonatomic) IBOutlet UIButton *bYearBtn;
@property (strong, nonatomic) IBOutlet UIButton *bMonthBtn;
@property (strong, nonatomic) IBOutlet UIButton *bDayBntn;
@property (strong, nonatomic) IBOutlet UIButton *bBtn;
@property (strong, nonatomic) IBOutlet UIButton *mMoneyBtn;

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *timeData;
- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation ReferViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}

- (void)initializeUserInterface {
    
    _dataSource = [NSMutableArray array];
    _timeData = [NSMutableArray array];
    
    self.titleImageView.image = [UIImage imageNamed:@"业务咨询logo"];
    [super initializeUserInterface];
    [self.view addSubview:self.tableView];
    self.persentField.delegate = self;
    self.moneyField.delegate = self;
    self.locationField.delegate = self;
    self.bigMoneyField.delegate = self;
    
}
- (IBAction)mmBtnEvent:(id)sender {
    
    self.mmView.hidden = NO;
    self.bigView.hidden = YES;
}

- (IBAction)bigBtnEvent:(id)sender {
    
    self.mmView.hidden = YES;
    self.bigView.hidden = NO;
}
#pragma mark - MMView methods
- (IBAction)yearBtnEvent:(id)sender {
    UIButton *btn = (UIButton *)sender;
    
    YearView *yearView = [[YearView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.mYearBtn setTitle:selection forState:UIControlStateNormal];
        _year = selection;
    }];
    
    if (![self.view.subviews containsObject:yearView]) {
        [self.view addSubview:yearView];
    }
    [self startAnimationWithView:yearView];
}
- (IBAction)monthBntEvent:(id)sender {
    
    UIButton *btn = (UIButton *)sender;
    
    MonthView *monthView = [[MonthView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.mMonthBtn setTitle:selection forState:UIControlStateNormal];
        _month = selection;
    }];
    
    if (![self.view.subviews containsObject:monthView]) {
        [self.view addSubview:monthView];
    }
    [self startAnimationWithView:monthView];
}
- (IBAction)dayBtnEvent:(id)sender {
    UIButton *btn = (UIButton *)sender;
    
    DayView *dayView = [[DayView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.mDayBtn setTitle:selection forState:UIControlStateNormal];
        _day = selection;
    }];
    
    if (![self.view.subviews containsObject:dayView]) {
        [self.view addSubview:dayView];
    }
    [self startAnimationWithView:dayView];
    
}
- (IBAction)deadlineBtnEvent:(id)sender {
    
    UIButton *btn = (UIButton *)sender;
    
    DuraingView *duraingView = [[DuraingView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.mBtn setTitle:selection forState:UIControlStateNormal];
        _during = selection;
    }];
    
    if (![self.view.subviews containsObject:duraingView]) {
        [self.view addSubview:duraingView];
    }
    [self startAnimationWithView:duraingView];
}

#pragma mark - BigView methdos
- (IBAction)reposndsToYearBtn:(id)sender {
    UIButton *btn = (UIButton *)sender;
    YearView *yearView = [[YearView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.bYearBtn setTitle:selection forState:UIControlStateNormal];
        _year = selection;
    }];
    
    if (![self.view.subviews containsObject:yearView]) {
        [self.view addSubview:yearView];
    }
    [self startAnimationWithView:yearView];
}
- (IBAction)reposndsToMonthBtn:(id)sender {
    UIButton *btn = (UIButton *)sender;
    MonthView *monthView = [[MonthView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.bMonthBtn setTitle:selection forState:UIControlStateNormal];
        _month = selection;
    }];
    
    if (![self.view.subviews containsObject:monthView]) {
        [self.view addSubview:monthView];
    }
    [self startAnimationWithView:monthView];
}
- (IBAction)reposndsToDayBtn:(id)sender {
    UIButton *btn = (UIButton *)sender;
    DayView *dayView = [[DayView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.bDayBntn setTitle:selection forState:UIControlStateNormal];
        _day = selection;
    }];
    
    if (![self.view.subviews containsObject:dayView]) {
        [self.view addSubview:dayView];
    }
    [self startAnimationWithView:dayView];
}
- (IBAction)reposndsToMoney:(id)sender {
    UIButton *btn = (UIButton *)sender;
    WhichMoneyView *moneyView = [[WhichMoneyView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.bBtn setTitle:selection forState:UIControlStateNormal];
        _money = selection;
    }];
    
    if (![self.view.subviews containsObject:moneyView]) {
        [self.view addSubview:moneyView];
    }
    [self startAnimationWithView:moneyView];
}
- (IBAction)respondsTomMoneyBtn:(id)sender {
    UIButton *btn = (UIButton *)sender;
    WhichMoneyView *moneyView = [[WhichMoneyView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(CGRectGetMidX(btn.frame) + 100, CGRectGetMinY(btn.frame) + 50, 170, 180)) sureBlock:^(NSString *selection) {
        [self.mMoneyBtn setTitle:selection forState:UIControlStateNormal];
        _money = selection;
    }];
    
    if (![self.view.subviews containsObject:moneyView]) {
        [self.view addSubview:moneyView];
    }
    [self startAnimationWithView:moneyView];
}

- (IBAction)sendMessageEvent:(id)sender {
    NSString *message = [NSString stringWithFormat:@"我希望于%@年%@月%@日,至%@附近网点取现,取现金额为%@万%@",_year,_month,_day,self.locationField.text,self.bigMoneyField.text,_money];
    NSString *mess = [message mutableCopy];
    [_dataSource insertObject:mess atIndex:0];
    
    [self getTimeNow];
    [self.tableView reloadData];
}
- (IBAction)sendBigMessageEvent:(id)sender {
    NSString *message = [NSString stringWithFormat:@"我希望于%@年%@月%@日,购买年华收益大于等于%@%%期限%@的产品,购买金额为%@万%@",_year,_month,_day,self.persentField.text,_during,self.moneyField.text,_money];
    
    NSString *mess = [message mutableCopy];
    [_dataSource insertObject:mess atIndex:0];
    [self getTimeNow];
    [self.tableView reloadData];
}
- (void)getTimeNow {

    NSDate * senddate = [NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm"];
    NSString * locationString = [dateformatter stringFromDate:senddate];
    
    //获得系统日期
    NSCalendar * cal = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit;
    NSDateComponents * conponent= [cal components:unitFlags fromDate:senddate];
    NSInteger year = [conponent year];
    NSInteger month = [conponent month];
    NSInteger day = [conponent day];
    NSString * nsDateString = [NSString stringWithFormat:@"%d-%d-%d",year,month,day];
    NSString *timeNow = [NSString stringWithFormat:@"%@ %@",nsDateString,locationString];
    NSString *time = [timeNow mutableCopy];
    [_timeData insertObject:time atIndex:0];
}
- (void)startAnimationWithView:(UIView *)view {

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    // 动画选项设定
    animation.duration = 0.3; // 动画持续时间
    animation.repeatCount = 1; // 重复次数
    
    // 缩放倍数
    animation.fromValue = [NSNumber numberWithFloat:0.3]; // 开始时的倍率
    animation.toValue = [NSNumber numberWithFloat:1.0]; // 结束时的倍率
    
    // 添加动画
    [view.layer addAnimation:animation forKey:@"scale-layer"];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if ([string isEqualToString:@"\n"]) {
        return YES;
    }
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (textField.tag == 100) {
        if ([toBeString length] > 4) {
            textField.text = [toBeString substringToIndex:4];
            
            return NO;
        }
    } else if (textField.tag == 101) {
        if ([toBeString length] > 5) {
            textField.text = [toBeString substringToIndex:5];
            return NO;
        }
    } else if (textField.tag == 102) {
        if ([toBeString length] > 25) {
            textField.text = [toBeString substringToIndex:25];
            return NO;
        }
    } else {
        if ([toBeString length] > 5) {
            textField.text = [toBeString substringToIndex:5];
            return NO;
        }
        
    }
    return YES;
}
#pragma mark - <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataSource.count * 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row % 2 == 0) {
        JLbusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewBusinessCellIndentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backView.layer.borderColor = COLOR(246, 239, 220, 1).CGColor;
        cell.backView.layer.borderWidth = 2;
        cell.backView.layer.masksToBounds = YES;
        cell.backView.layer.cornerRadius = 5;
        cell.contentLabel.text = _dataSource[indexPath.row/2];
        cell.time.text = _timeData[indexPath.row/2];
        return cell;
    } else {
        JLBigTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewBigCellIndentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backView.layer.masksToBounds = YES;
        cell.backView.layer.cornerRadius = 5;
        cell.time.text = _timeData[(indexPath.row-1)/2];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row % 2 == 0) {
        return 120;
    } else  {
        return 53;
    }
}

#pragma mark - getter
- (UITableView *)tableView {

    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:DHFlexibleFrame(CGRectMake(100, 544, 499, 329)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"JLbusinessTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kUITableViewBusinessCellIndentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"JLBigTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kUITableViewBigCellIndentifier];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
